package com.skplanet.rade.controller.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.skplanet.rade.domain.Tag;
import com.skplanet.rade.domain.User;
import com.skplanet.rade.domain.UserTag;
import com.skplanet.rade.service.TagService;
import com.skplanet.rade.service.UserService;
import com.skplanet.rade.util.TransJSON;


@Controller
@RequestMapping( value = "/user" )
public class UserController {
	private static final String SC_OK = "OK";
	private static final String SC_FAIL = "FAIL";
	private static final int MAX_TAG = 3;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private TagService tagService;
	
	@Autowired
	private MessageSource messageSource;
	
	@RequestMapping( value = "/login", method = RequestMethod.POST)
	public void login( 
			@RequestParam(value="email", required=true)String email,
			@RequestParam(value="password", required=true)String password,
			Model model
			){
		
		if (userService.checkEqualsPassword( userService.getUserFromEmail(email), password))
		{
			User user = userService.getUserFromEmail(email);
			model.addAttribute("response", TransJSON.InfoToJSON(user));
			// model.addAttribute("response", SC_OK);
		}
		else
		{
			model.addAttribute("response", SC_FAIL);
		}
	}
	
	@RequestMapping( value = "/join", method = RequestMethod.POST)
	public void join( 
			@RequestParam(value="email", required=true)String email,
			@RequestParam(value="password", required=true)String password,
			@RequestParam(value="nickname", required=true)String nickname,
			@RequestParam(value="tag1", defaultValue="null", required=false)String tagKeyword0,
			@RequestParam(value="tag2", defaultValue="null", required=false)String tagKeyword1,
			@RequestParam(value="tag3", defaultValue="null", required=false)String tagKeyword2,
			Model model
			) 
	{
		if ( (userService.emailExists(email) == false) && (userService.nicknameExists(nickname) == false) ) {
			User user = userService.initUser(email, password, nickname);
			userService.addUserTag(user, tagKeyword0, tagKeyword1, tagKeyword2);
			userService.addUser(user);
			model.addAttribute("response", SC_OK);
		}
		else
		{
			model.addAttribute("response", SC_FAIL);
		}
	}
	
	@RequestMapping( value = "/main", method = RequestMethod.GET )
	public void main(
			Model model
			)
	{
		List<Tag> topNTagList = tagService.getNTagList(40);
		model.addAttribute("tagCloud", TransJSON.InfoToJSON(topNTagList));
	}
			
	@RequestMapping( value = "/setting-select", method = RequestMethod.GET )
	public void settingSelect(
			@RequestParam(value="email", required=true)String email,
			Model model
			) 
	{
		User user = userService.getUserFromEmail(email);

		List<UserTag> topNUserTagList = userService.getNUserTagListFromUser(user, MAX_TAG);
		
		model.addAttribute("tags", TransJSON.InfoToJSON(topNUserTagList));
	}
	
	@RequestMapping( value = "/setting-update", method = RequestMethod.POST)
	public void settingUpdate(
			@RequestParam(value="email", required=true)String email,
			@RequestParam(value="password", defaultValue="null", required=false)String password,
			Model model ) {
		
		User user = userService.getUserFromEmail(email);

		if(password != null)
		{
			userService.changePassword(user, password);
		}
		
		userService.updateUser(user);
		
		model.addAttribute("response", SC_OK);
	}
}